package cn.gson.crm.controller;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crm.model.Jdbc;

@WebServlet("/selector")
public class SelectorServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = request.getParameter("type");
		String sql = "";/*select * from province*/
		if("city".equalsIgnoreCase(type)){
			String provinceId = request.getParameter("province");
			sql = "select * from city where province_id = " + Integer.parseInt(provinceId);
		}else if("area".equalsIgnoreCase(type)){
			String cityId = request.getParameter("city");
			sql = "select * from area where city_id = " + Integer.parseInt(cityId);
		}
		
		try {
			List<Map<String, Object>> data = Jdbc.getInstace().executeQueryForList(sql);
			/*if(type == null){
				request.setAttribute("provinces", data);
				request.getRequestDispatcher("city.jsp").forward(request, response);
			}else{*/
				response.setContentType("application/json;charset=utf-8");
				
				//把结果转换成JSON字符串
				String jsonStr = JSONArray.toJSONString(data);
				//输出结果
				response.getWriter().append(jsonStr);
			/*}*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
